<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style type="text/css">
            .tangram-suggestion-main {
                z-index: 999999;
            }
        </style>
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">活动分类</label>
                        <div class="layui-input-block">
                            <div name="category_id" id="category_id" value="" ></div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">活动名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">讲师名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="teacher_name" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">讲师简介</label>
                        <div class="layui-input-block">
                            <textarea name="teacher_desc" id="teacher_desc" cols="30" rows="10" class="layui-textarea"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">省/市/区</label>
                        <div class="layui-input-block" id="area">
                            <div class="layui-input-inline" style="width: 200px;">
                                <select name="province" class="province-selector" >
                                    <option value="">请选择省</option>
                                </select>
                            </div>
                            <div class="layui-input-inline" style="width: 200px;">
                                <select name="city" class="city-selector" >
                                    <option value="">请选择市</option>
                                </select>
                            </div>
                            <div class="layui-input-inline" style="width: 200px;">
                                <select name="district" class="county-selector">
                                    <option value="">请选择区</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">详细地址</label>
                        <div class="layui-input-block">
                            <input type="text" id="address" name="address" value="" autocomplete="off" class="layui-input">
                            <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:100%;height:auto; display:none;"></div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">经纬度选择</label>
                        <div class="layui-input-block" id="map" style="height: 400px;">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">经纬度</label>
                        <div class="layui-input-block">

                            <div class="layui-input-inline">
                                <input type="text" name="longitude" id="longitude" value="" placeholder="经度" class="layui-input">
                            </div>

                            <div class="layui-input-inline">
                                <input type="text" name="latitude" id="latitude" value="" placeholder="纬度" class="layui-input">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">开始时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="start_time" id="start_time" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">结束时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="end_time" id="end_time" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">报名截止时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="deadline" id="deadline" value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">活动名额</label>
                        <div class="layui-input-block">
                            <input type="number" name="limit_count" value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">报名费用</label>
                        <div class="layui-input-block">
                            <input type="text" name="price" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">活动图片</label>
                        <div class="layui-input-block">
                            <img class="img-3" src=""/>
                            <input type="text" style="display:none" name="img_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="image" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="attachment-choose-image" permission="app.admin.upload.attachment">
                                <i class="layui-icon layui-icon-align-left"></i>选择图片
                            </button>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">活动内容</label>
                        <div class="layui-input-block">
                            <textarea name="content" id="content" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        通过
                    </button>
                    <button type="submit" class="pear-btn pear-btn-danger pear-btn-md" lay-submit="" lay-filter="reject">
                        驳回
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script type="text/javascript" src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=YgEjHRf7aPtgdarZHmLdoINX5AwDfCSy"></script>
        
        <script>

            // 百度地图API功能
            function G(id) {
                return document.getElementById(id);
            }

            var map = new BMapGL.Map("map");
            map.centerAndZoom("深圳",12);

            var ac = new BMapGL.Autocomplete(    //建立一个自动完成的对象
                {"input" : "address"
                    ,"location" : map
                });

            ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
                var str = "";
                var _value = e.fromitem.value;
                var value = "";
                if (e.fromitem.index > -1) {
                    value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                }
                str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                value = "";
                if (e.toitem.index > -1) {
                    _value = e.toitem.value;
                    value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                }
                str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                G("searchResultPanel").innerHTML = str;
            });

            var myValue;
            ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
                var _value = e.item.value;
                myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

                setPlace();
            });

            map.addEventListener('onclick', function(e) {
                map.clearOverlays();    //清除地图上所有覆盖物
                map.centerAndZoom(e.latlng, 18);
                map.addOverlay(new BMapGL.Marker(e.latlng));    //添加标注
                G('longitude').setAttribute('value', e.latlng.lng)
                G('latitude').setAttribute('value', e.latlng.lat)
            })

            function setPlace(){
                map.clearOverlays();    //清除地图上所有覆盖物
                function myFun(){
                    var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                    map.centerAndZoom(pp, 18);
                    map.addOverlay(new BMapGL.Marker(pp));    //添加标注
                    G('longitude').setAttribute('value', pp.lng)
                    G('latitude').setAttribute('value', pp.lat)
                }
                var local = new BMapGL.LocalSearch(map, { //智能搜索
                    onSearchComplete: myFun
                });
                local.search(myValue);
            }

            var editor;

            // 相关接口
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/activity/select" + location.search;
            const UPDATE_API = "/app/admin/activity/update";
            const SELECT_NAME_API = "/app/admin/user/searchname";

            var timer;
            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                let $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            } else {
                                obj.attr("value", value);
                            }

                            if (key == 'img_url') {
                                obj.prev().attr('src', value)
                            }

                            if (key == 'address') {
                                ac.setInputValue(value)

                                myValue = value;

                                setPlace()
                            }

                        });

                        // image
                        let input = $("#image").prev();
                        input.prev().attr("src", input.val());
                        layui.$("#attachment-choose-image").on("click", function() {
                            parent.layer.open({
                                type: 2,
                                title: "选择附件",
                                content: "/app/admin/upload/attachment?ext=jpg,jpeg,png,gif,bmp",
                                area: ["95%", "90%"],
                                success: function (layero, index) {
                                    parent.layui.$("#layui-layer" + index).data("callback", function (data) {
                                        input.val(data.url).prev().attr("src", data.url);
                                    });
                                }
                            });
                        });
                        layui.upload.render({
                            elem: "#image",
                            url: "/app/admin/upload/image",
                            acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                            done: function (res) {
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                            }
                        });


                        layui.use(['tinymce'], function() {
                            var tinymce = layui.tinymce
                            editor = tinymce.render({
                                elem: "#content",
                                images_upload_url: "/app/admin/upload/image",
                                height: 400
                            });

                            editor.setContent(res.data[0].content)
                        });

                        // 字段 类别 category
                        layui.use(["jquery", "xmSelect"], function() {
                            layui.$.ajax({
                                url: "/app/admin/activity-category/tree",
                                dataType: "json",
                                success: function (res) {
                                    let value = layui.$("#category_id").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        tree: {
                                            show: true,
                                            strict: false,
                                            expandedKeys: true
                                        },
                                        el: "#category_id",
                                        name: "category_id",
                                        initValue: initValue,
                                        data: res.data,
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                    });
                                    if (res.code) {
                                        return layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });

                        layui.use(['area'], function () {
                            var area = layui.area;
                            var $ = layui.jquery;

                            area.render({
                                elem: '#area',
                                data: {
                                  province: res.data[0].province,
                                    city: res.data[0].city,
                                    county: res.data[0].district,
                                },
                                change: function (res) {
                                    if (res.city != '') {
                                        map.centerAndZoom(res.city,12);
                                    }
                                }
                            });


                        });
                        
                        // 字段 发布时间 created_time
                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#start_time",
                                type: "datetime",
                            });
                        })

                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#end_time",
                                type: "datetime",
                            });
                        })

                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#deadline",
                                type: "datetime",
                            });
                        })
                        
                        
                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }

                        layui.form.render()
                    }
                });
            });


            layui.$('#nickname').keyup(function() {
                clearTimeout(timer)
                var value = layui.$(this).val()
                timer = setTimeout(function() {
                    layui.$.ajax({
                        url: SELECT_NAME_API,
                        type: "POST",
                        dateType: "json",
                        data: {name: value},
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            layui.$('#user_id').html(res.data.html)
                            layui.form.render()
                        }
                    });
                }, 500);
            })

            //提交事件
            layui.use(["form", "popup"], function () {
                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                layui.form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    data.field.content = editor.getContent()
                    data.field.is_enable = 1
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });

                layui.form.on("submit(reject)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    data.field.content = editor.getContent()
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>

</html>
